/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: Frank
 * 系统版本: 1.0.0
 * 开发人员: Frank
 * 开发时间: 2019/12/12 11:17
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.uaa.service;

import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.uaa.bean.EmployeeBean;
import com.spring.arch.uaa.errorcode.UaaErrorCode;
import com.spring.arch.uaa.model.*;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.uaa.bean.EmployeeBean;
import com.spring.arch.uaa.model.*;
import com.spring.arch.uaa.repository.OrganizationEmployeeRepository;
import com.spring.arch.uaa.repository.OrganizationEmployeeRoleRepository;
import com.spring.arch.uaa.repository.TenantEmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static com.spring.arch.uaa.errorcode.UaaErrorCode.E2001001002;

/**
 * 租户人员服务层接口
 *
 * @author Frank
 * @version 1.0.0.1
 * @since 2019/12/12 11:17
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EmployeeService {

    @Autowired
    private UserService userService;
    @Autowired
    private TenantEmployeeRepository tenantEmployeeRepository;
    @Autowired
    private TenantEmployeeService tenantEmployeeService;
    @Autowired
    private OrganizationEmployeeRepository organizationEmployeeRepository;
    @Autowired
    private OrganizationEmployeeRoleRepository organizationEmployeeRoleRepository;

    public void delete(UUID id) {
        final TenantEmployee employee = tenantEmployeeRepository.findById(id)
                .orElseThrow(() -> ExceptionHolder.serviceException(UaaErrorCode.E2001001002, String.valueOf(id)));
        employee.setDisable(true);
        tenantEmployeeRepository.save(employee);
    }

    public TenantEmployee save(EmployeeBean bean) {
        @NotNull Tenant tenant = bean.getTenant();
        Project project = bean.getProject();
        // 创建人员信息
        TenantEmployee employee = tenantEmployeeService.save(bean.getTenantEmployee(), null);
        Organization organization = bean.getOrganization();
        if (organization != null) {
            Optional<OrganizationEmployee> optional = organizationEmployeeRepository.findByOrganizationAndEmployee(organization, employee);
            if (!optional.isPresent() || optional.get().getUser() == null) {
                // 创建用户
                User user = userService.createUser(employee);
                if (!optional.isPresent()) {
                    // 创建组织、人员、用户关联
                    OrganizationEmployee organizationEmployee = new OrganizationEmployee(organization, employee, user);
                    organizationEmployeeRepository.save(organizationEmployee);
                    optional = Optional.of(organizationEmployee);
                }
            }

            final OrganizationEmployee orgEmp = optional.get();
            List<Role> roles = bean.getRoles();
            if (!CollectionUtils.isEmpty(roles)) {
                roles.forEach(role -> {
                    // 创建组织、人员、用户、角色关联
                    OrganizationEmployeeRole orgEmpRole = new OrganizationEmployeeRole(orgEmp, role, tenant, organization, project);
                    organizationEmployeeRoleRepository.save(orgEmpRole);
                });
            }
        }
        return employee;
    }
}
